/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  internal class ReactiveOperationInfo<T>
  {
    internal ReactiveOperationInfo(
      SourcePosition sourcePosition,
      long expectedMoveCount,
      int expectedFetchCount,
      bool result )
    {
      this.SourcePosition = sourcePosition;
      this.ExpectedMoveCount = expectedMoveCount;
      this.ExpectedFetchCount = expectedFetchCount;
      this.ActualMoveCount = null;
      this.FetchedItems = null;
      this.Result = result;
    }

    internal ReactiveOperationInfo( JumpMoveAndFetchParameters parameters, bool result )
    {
      this.m_parameters = parameters;
      this.ActualMoveCount = null;
      this.FetchedItems = null;
      this.Result = result;
    }

    internal SourcePosition SourcePosition
    {
      get
      {
        return m_parameters.SourcePosition;
      }
      set
      {
        //Changes to this value should not happen when the operation is already
        //complete
        CheckIsNotCompleted();
        m_parameters = m_parameters.CreateWithNewSourcePosition( value );
      }
    }

    internal long ExpectedMoveCount
    {
      get
      {
        return m_parameters.ExpectedMoveCount;
      }
      set
      {
        //Changes to this value should not happen when the operation is already
        //complete
        CheckIsNotCompleted();
        m_parameters = m_parameters.CreateWithNewMoveCount( value );
      }
    }

    internal int ExpectedFetchCount
    {
      get
      {
        return m_parameters.ExpectedFetchCount;
      }
      set
      {
        //Changes to this value should not happen when the operation is already
        //complete
        CheckIsNotCompleted();
        m_parameters = m_parameters.CreateWithNewFetchCount( value );
      }
    }

    internal bool ApproximateMove
    {
      get
      {
        return m_parameters.ApproximateMove;
      }
      set
      {
        //Changes to this value should not happen when the operation is already
        //complete
        CheckIsNotCompleted();
        m_parameters = m_parameters.CreateWithNewApproximateMoving( value );
      }
    }


    internal long ExpectedDisplacement
    {
      get
      {
        // Move displacement (see ResultsExtensions.GetMoveDisplacement)
        long totalDisplacement = this.ExpectedMoveCount;

        // Fetch displacement (see ResultsExtensions.GetFetchDisplacement)
        if( this.ExpectedFetchCount != 0 )
        {
          totalDisplacement += ( this.ExpectedFetchCount - DataUtils.GetUnit( this.ExpectedFetchCount ) );
        }

        return totalDisplacement;
      }
    }

    internal JumpMoveAndFetchParameters Parameters
    {
      get
      {
        return m_parameters;
      }
    }

    private void CheckIsNotCompleted()
    {
    }

#if DEBUG
    public override string ToString()
    {
      string fetchedItemStr = "null";
      if( this.FetchedItems != null )
      {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach( T item in this.FetchedItems )
        {
          if( sb.Length > 0 )
          {
            sb.Append( "," );
          }

          DataPath path = item as DataPath;
          sb.Append( path.LastChild.ToString() );
        }
        fetchedItemStr = sb.ToString();
      }
      string approxParam = this.ApproximateMove ? " approx" : "precise";
      return string.Format( "Operation: Source={0}, Move ={1}{5}, Fetch={2}, EndPosition={3}, FetchedItems=({4})", this.SourcePosition.ToString().PadRight( 70 ), this.ExpectedMoveCount, this.ExpectedFetchCount, this.EndPosition, fetchedItemStr, approxParam );
    }
#endif

    private JumpMoveAndFetchParameters m_parameters;
    internal Nullable<long> ActualMoveCount;
    internal ICollection<T> FetchedItems;
    internal bool Result;
    internal SourcePosition EndPosition;
    internal RelativeOffset EndPositionOffset;
    internal EstimateInt64 TotalCount;
  }
}
